package edu.hawaii.duedates.webapp;

import org.apache.wicket.Request;
import org.apache.wicket.protocol.http.WebSession;
import org.apache.wicket.util.thread.Task;
import edu.hawaii.duedates.datastore.DueDatesConfig;
import edu.hawaii.duedates.datastore.User;

/**
 * Each user of the DueDates application is given their own instance of DueDatesSession to keep
 * track of their data.
 * 
 * @author Daniel Arakaki, Jeho Jung, Arthur Shum, Daniel Tian.
 */
public class DueDatesSession extends WebSession {

  /** Support serialization. */
  private static final long serialVersionUID = 1L;

  /** The logged-in user's data. */
  private User user;
  
  /** The SMTP server to use to send email. */
  private String smtpServer;
  
  /** Specifies whether or not the current user is logged-in. */
  private boolean isLoggedIn;
  
  /** All of the configuration settings. */
  private DueDatesConfig config;
  
  /** Task to run things periodically. Transient b/c don't want to serialize. */
  private transient Task task;
  
  /** Indicates whether or not alerts are enabled. */
  private boolean alertsEnabled;
  
  /**
   * Create a session to hold each user's data.
   * 
   * @param request The HTTP request.
   */
  public DueDatesSession(Request request) {
    super(request);
  }
  
  /**
   * Checks whether or not the current user is logged-in.
   * 
   * @return true if logged in, false otherwise.
   */
  public boolean isLoggedIn() {
    return this.isLoggedIn;
  }
  /**
   * Sets the current user's logged-in status to true.
   */
  public void setIsLoggedIn() {
    this.isLoggedIn = true;
  }
  /**
   * Returns the logged-in user's data.
   * 
   * @return the logged-in user's data.
   */
  public User getUser() {
    return this.user;
  }
  /**
   * Sets this session to store the specified user data.
   * 
   * @param user The user data to work with in this session.
   */
  public void setUser(User user) {
    this.user = user;
  }
  /**
   * Returns the SMTP server to use.
   * 
   * @return the SMTP server to use.
   */
  public String getSmtpServer() {
    return this.smtpServer;
  }
  /**
   * Sets the SMTP server.
   * 
   * @param smtpServer The server to use.
   */
  public void setSmtpServer(String smtpServer) {
    this.smtpServer = smtpServer;
  }

  /**
   * Returns the configuration settings.
   * 
   * @return The configuration settings.
   */
  public DueDatesConfig getDueDatesConfig() {
    return this.config;
  }

  /**
   * Sets the configuration settings.
   * 
   * @param config The configuration settings to set.
   */
  public void setDueDatesConfig(DueDatesConfig config) {
    this.config = config;
  }
  
  /**
   * Returns a new task.
   * 
   * @return A new task.
   */
  public Task getNewTask() {
    this.task = new Task("alert");
    return this.task;
  }

  /**
   * Returns the current task.
   * 
   * @return The current task.
   */
  public Task getTask() {
    return this.task;
  }
  
  /**
   * Checks whether or not alerts are enabled (i.e., the task is running).
   * 
   * @return Whether or not alerts are enabled.
   */
  public boolean alertsEnabled() {
    return this.alertsEnabled;
  }
  
  /**
   * Sets the alertsEnabled status flag.
   * 
   * @param value True if alerts are enable, false if they are not.
   */
  public void setAlertsEnabled(boolean value) {
    this.alertsEnabled = value;
  }
  
}
